package com.ruoyi.bom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.bom.domain.SysDeptBean;
import com.ruoyi.bom.domain.TqlHllKingdee;
import com.ruoyi.bom.domain.TqlHllShop;
import com.ruoyi.bom.mapper.SysDeptMapper;
import com.ruoyi.bom.mapper.TqlHllKingdeeMapper;
import com.ruoyi.bom.mapper.TqlHllShopMapper;
import com.ruoyi.bom.remoteService.KingdeeApiService;
import com.ruoyi.bom.service.TqlHllShopService;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.web.domain.CommonEntity;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author 86198
* @description 针对表【tql_hll_shop】的数据库操作Service实现
* @createDate 2024-01-16 18:36:42
*/
@Service
public class TqlHllShopServiceImpl implements TqlHllShopService {


    @Autowired
    private KingdeeApiService kingdeeApiService;
    @Resource
    private TqlHllShopMapper tqlHllShopMapper;

    @Resource
    private TqlHllKingdeeMapper tqlHllKingdeeMapper;

    @Override
    public List<TqlHllShop> selectShopList(Map<String, String> params) {
        return tqlHllShopMapper.selectShopList(params);
    }

    @Override
    public List<TqlHllShop> selectShopListAll() {
        return tqlHllShopMapper.selectShopListAll();
    }

    @Override
    public boolean open(Long shopId, Long shopCode) {
        //0.验证有么有开通过
        //1.用shopId 查到对应的kingdee 的fnumber
        //select * from sys_dept where relateId='76450932'
        SysDeptBean dept = tqlHllShopMapper.selectSysDept(shopCode);
        if(dept == null) return false;
        //select * from sys_dept where pdeptCode='BU1010010013'
        int count = tqlHllShopMapper.selectSysDeptByPdeptCode(dept.getDeptCode());
        if(count==0) {
            //如果没有就给他添加子集
            List<SysDeptBean> subs = getSubSysDeptList(dept);
            tqlHllShopMapper.insertBatch(subs);
            //批量插入
            for(SysDeptBean each:subs) {
//                int rtn=tqlHllShopMapper.insertSingle(each);
                int count1=tqlHllShopMapper.selectSysDeptByPdeptCode(each.getDeptCode());
                if(count1==0)  {
                    //如果下面还是没有数据请从接口获取
                    List<CommonEntity> list2= kingdeeApiService.findDeptByParentDeptFnumber(each.getDeptCode());
                    List<SysDeptBean> subs1=getSysDeptBeanByCommonEntity(list2,each);
                    tqlHllShopMapper.insertBatch(subs1);
                }
            }
        }
        String createdBy = SecurityUtils.getUsername();
        QueryWrapper<TqlHllKingdee> wrapper = new QueryWrapper<>();
        wrapper.eq("shopCode", shopCode);
        wrapper.eq("shopId", shopId);
        wrapper.last("limit 1");
        TqlHllKingdee kingdee = tqlHllKingdeeMapper.selectOne(wrapper);
        if (ObjectUtils.isNotEmpty(kingdee)) {
            throw new ServiceException("该店铺已开通！");
        }
        kingdee =new TqlHllKingdee();
        kingdee.setShopCode(shopCode);
        kingdee.setShopId(shopId);
        kingdee.setKingdeeDeptId(dept.getDeptId());
        kingdee.setKingdeeDeptCode(dept.getDeptCode());
        kingdee.setFlag(1);
        kingdee.setCreator(createdBy);
        kingdee.setOrigin("hll");
        tqlHllKingdeeMapper.insert(kingdee);
        return true;
    }

    @Override
    public boolean sync(String deptCode) {
        List<SysDeptBean> allSubs = tqlHllShopMapper.getSysDeptByDeptCode(deptCode);
        if (allSubs == null) {
            throw new ServiceException("该门店下无厨部和服务部门！");
        }
        List<String> deptCodes = allSubs.stream().map(SysDeptBean::getDeptCode).collect(Collectors.toList());
        List<SysDeptBean> subs = tqlHllShopMapper.getTopSysDeptByDeptCode(deptCode);
        if (!subs.isEmpty()) {
            for (SysDeptBean deptBean : subs) {
                //如果下面还是没有数据请从接口获取
                List<CommonEntity> list2 = kingdeeApiService.findDeptByParentDeptFnumber(deptBean.getDeptCode());
                if (!list2.isEmpty()) {
                    list2 = list2.stream().filter(item -> !deptCodes.contains(item.getFnumber()))
                            .collect(Collectors.toList());
                    List<SysDeptBean> subs1 = getSysDeptBeanByCommonEntity(list2,deptBean);
                    tqlHllShopMapper.insertBatch(subs1);
                }
            }
        }
        return true;
    }

    private List<SysDeptBean> getSysDeptBeanByCommonEntity(List<CommonEntity> list2, SysDeptBean parent) {
        if(CollectionUtils.isEmpty(list2))return  null;
        List<SysDeptBean> list=new ArrayList<>();
        for(CommonEntity entity:list2) {
            SysDeptBean bean=new SysDeptBean();
            bean.setPdeptName(entity.getPfname());
            bean.setPdeptCode(entity.getPfnumber());
            if (StringUtils.isBlank(entity.getPfnumber())){
                bean.setPdeptName(parent.getDeptName());
            }
            if (StringUtils.isBlank(entity.getPfnumber())){
                bean.setPdeptCode(parent.getDeptCode());
            }
            bean.setDeptName(entity.getFname());
            bean.setDeptCode(entity.getFnumber());
            bean.setFlag(2);
            bean.setOrderNum(3);
            bean.setParentId(parent.getDeptId());
            bean.setAncestors(parent.getAncestors()+","+parent.getDeptId());
            list.add(bean);
        }
        return list;
    }


    private List<SysDeptBean> getSubSysDeptList(SysDeptBean parent) {

        List<SysDeptBean> list=new ArrayList<>();
        SysDeptBean sysDeptBean1=new SysDeptBean();
        sysDeptBean1.setParentId(parent.getDeptId());
        sysDeptBean1.setAncestors(parent.getAncestors()+","+parent.getDeptId());
        sysDeptBean1.setDeptName(parent.getDeptName()+"服务部");
        sysDeptBean1.setOrderNum(1);
        sysDeptBean1.setDeptCode(parent.getDeptCode()+"001");
        sysDeptBean1.setPdeptName(parent.getDeptName());
        sysDeptBean1.setPdeptCode(parent.getDeptCode());
        sysDeptBean1.setFlag(1);
        list.add(sysDeptBean1);
        SysDeptBean sysDeptBean2=new SysDeptBean();
        sysDeptBean2.setParentId(parent.getDeptId());
        sysDeptBean2.setAncestors(parent.getAncestors()+","+parent.getDeptId());
        sysDeptBean2.setDeptName(parent.getDeptName()+"厨部");
        sysDeptBean2.setOrderNum(2);
        sysDeptBean2.setDeptCode(parent.getDeptCode()+"002");
        sysDeptBean2.setPdeptCode(parent.getDeptCode());
        sysDeptBean2.setPdeptName(parent.getDeptName());
        sysDeptBean2.setPdeptCode(parent.getDeptCode());
        sysDeptBean2.setFlag(1);
        list.add(sysDeptBean2);
        return list;

    }
    public static boolean getFloat(String str) {
        try {
            Float.parseFloat(str);
            return true;
        } catch (NumberFormatException e) {
            // 异常处理
            e.printStackTrace();
            return false;
        }
    }

}




